package com.hean.activity;

import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.hean.entity.Orientation;
import com.hean.view.SensorView;

public class SensorActivity extends Activity {
	private TextView tvYaw;
	private TextView tvPitch;
	private TextView tvRoll;

	private SensorManagerSimulator sensorManager;
	// private SensorManager sensorManger;

	private Orientation orientation;
	private SensorView view;

	private SensorEventListener listener = new SensorEventListener() {
		@Override
		public void onSensorChanged(SensorEvent event) {
			float[] values = event.values;
			tvYaw.setText(String.valueOf(values[0]));
			tvPitch.setText(String.valueOf(values[1]));
			tvRoll.setText(String.valueOf(values[2]));
			orientation.set(values);
			view.invalidate();
		}


		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
			
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sensor);

		tvYaw = (TextView) findViewById(R.id.sensor_yaw);
		tvPitch = (TextView) findViewById(R.id.sensor_pitch);
		tvRoll = (TextView) findViewById(R.id.sensor_roll);

		view = (SensorView) findViewById(R.id.sensor_view);
		orientation = new Orientation(0, 0, 0);
		view.setOrientation(orientation);

		// sensorManger = (SensorManager) getSystemService(SENSOR_SERVICE);
		sensorManager = SensorManagerSimulator.getSystemService(
				SensorActivity.this, SENSOR_SERVICE);
		sensorManager.connectSimulator();

		Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		sensorManager.registerListener(listener, sensor,
				SensorManagerSimulator.SENSOR_DELAY_UI);

	}
}
